CharacterCreate = { }

local fadeoutdelay = 1

CharacterCreateLinks = {

	{ "CREATE CHARACTER", function()

		for k, v in pairs( CharacterCreate.MenuLinks ) do
			CharacterCreate.MenuLinks[k]:FadingOut( 150 );
			CharacterCreate.MenuLinks[k]:FadeDelay( fadeoutdelay );
			
			fadeoutdelay = fadeoutdelay + .2;

			timer.Simple( 5, function() CharacterCreate.MenuLinks[k]:Remove() end );
			
		end			

		timer.Simple( 4, function() event.StageOneCharacterCreation() end )
						
	end, ScrH() - 200 },
	
	
	{ "LOAD CHARACTER", function() 
	
		for k, v in pairs( CharacterCreate.MenuLinks ) do
			CharacterCreate.MenuLinks[k]:FadingOut( 150 );
			CharacterCreate.MenuLinks[k]:FadeDelay( fadeoutdelay );
			
			fadeoutdelay = fadeoutdelay + .2;

			timer.Simple( 5, function() CharacterCreate.MenuLinks[k]:Remove() end );
			
		end			

		timer.Simple( 4, function() event.CreateChooseMenu() end )
	
	end, ScrH() - 180 },
	
	
	{ "OPTIONS", function()
	

	end , ScrH() - 160 },
	
	
	{ "DISCONNECT", function() 
	
		RunConsoleCommand( "disconnect" );
	
	end , ScrH() - 120 },
	
	
	
}

surface.CreateFont( "Coolvetica", 72, 800, true, false, "OpeningName" );
surface.CreateFont( "Coolvetica", 40, 800, true, false, "OpeningYear" );
surface.CreateFont( "Mailart Rubberstamp", 25, 800, true, false, "FFont" );

function event.StartMenu()

	if( IntroFade > 0 ) then
		TS.WaitingToMakeCharacterMenu = true;
		return;
	end
	CharacterCreate.MenuLinks = { }
	
	local fadeindelay = 0 

	for k, v in pairs( CharacterCreateLinks ) do
	
		CharacterCreate.MenuLinks[k] = vgui.CreateLink();
		CharacterCreate.MenuLinks[k]:SetText( v[1], "NewChatFont", 1 );
		CharacterCreate.MenuLinks[k]:SetPos( ( ScrW() * -.15 - 2 ) + ( ( ScrW() * .15 + 25 ) ), v[3] );
		CharacterCreate.MenuLinks[k]:FadingIn( 150 );
		CharacterCreate.MenuLinks[k]:FadeDelay( fadeindelay );
		CharacterCreate.MenuLinks[k].Action = v[2];

		fadeindelay = fadeindelay + .9;
		
	end
	
	gui.EnableScreenClicker( true );
	
end

function event.StageOneCharacterCreation()

	LocalPlayer():GetTable().CharCreate = true

	local y = 0
	local fadeindelay = 0
	
	CharacterCreate.FactionLink = { }	

	CharacterCreate.Panel = CreateBPanel( nil, ScrW() / 2 - 150, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 150, 200 );
	CharacterCreate.Panel:CanClose( false );
	CharacterCreate.Panel:CanDrag( false );
	CharacterCreate.Panel:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	for k, v in pairs( HL2Factions ) do
	
		if( v.IsDefault or ( string.find( LocalPlayer():GetNWString( "Flags" ), v.Flags ) ) ) then
		
			CharacterCreate.FactionLink[k] = vgui.CreateLink();
			CharacterCreate.FactionLink[k]:SetParent( CharacterCreate.Panel );
			CharacterCreate.FactionLink[k]:SetText( v.Name, "NewChatFont", 1 );
			CharacterCreate.FactionLink[k]:SetPos( 25, 15 + y );
			CharacterCreate.FactionLink[k]:FadingIn( 145 );
			CharacterCreate.FactionLink[k]:FadeDelay( fadeindelay );
			CharacterCreate.FactionLink[k].Action = function() event.StageTwoCharacterCreation( k )
			
				for n, m in pairs( CharacterCreate.FactionLink ) do
					CharacterCreate.FactionLink[n]:Remove()
				end	
				
				CharacterCreate.Panel:Remove()	
				
			end	
			
		end
		
		fadeindelay = fadeindelay + .4;
		y = y + 20;

	end
	
	gui.EnableScreenClicker( true );

end

surface.CreateFont( "Disintegration", 35, 1800, true, false, "FactionFont" );

function event.StageTwoCharacterCreation( faction )

	gui.EnableScreenClicker( true );
	
	CharacterCreate.Header = CreateBPanel( nil, ScrW() / 2 - 225, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 360, 100 );
	CharacterCreate.Header.PaintHook = function( self )
	
		surface.SetMaterial( Material( "stampy" ) );
		surface.SetDrawColor( 255, 255, 255, 80 );
		surface.DrawTexturedRect( self:GetWide() - 70, self:GetTall() - 70, 64, 64 );
			
	end	
	
	CharacterCreate.FactionNameLabel = vgui.CreateFIFOLabel();
	CharacterCreate.FactionNameLabel:SetParent( CharacterCreate.Header )
	CharacterCreate.FactionNameLabel:FadingIn( 180 );
	CharacterCreate.FactionNameLabel:FadeDelay( .5 );
	CharacterCreate.FactionNameLabel:SetText( HL2Factions[faction].Name, "FFont", 1 )
	CharacterCreate.FactionNameLabel:SetPos( 5, 4 )
	
	local xoffset, yoffset = CharacterCreate.Header:GetPos()
	
	CharacterCreate.Frame = CreateBPanel( nil, xoffset, yoffset + 90, 360, 250 );

	local x, y = CharacterCreate.Frame:GetPos()
	
	CharacterCreate.LinkFields = { }
	CharacterCreate.Fields = { }
	CharacterCreate.FadeDelay = 1
	CharacterCreate.yoffset = 0

	local modelx = 0
	local modely = 0
	
	local fields =	{
	
		{ "Name", function()
		
			CharacterCreate.NameEntry = vgui.Create( "DTextEntry", CharacterCreate.Frame );
			CharacterCreate.NameEntry:SetPos( x + 55, y + 15 );
			CharacterCreate.NameEntry:SetSize( 150, 15 );
			CharacterCreate.NameEntry:MakePopup();
			
			table.insert( CharacterCreate.Fields, CharacterCreate.NameEntry )
			
		end	 },
		
		{ "Age", function() 
		
			CharacterCreate.AgeEntry = vgui.Create( "DTextEntry", CharacterCreate.Frame );
			CharacterCreate.AgeEntry:SetPos( x + 45, y + 45 );
			CharacterCreate.AgeEntry:SetSize( 25, 15 );
			CharacterCreate.AgeEntry:MakePopup();
			
			table.insert( CharacterCreate.Fields, CharacterCreate.AgeEntry )
		
		end	},
		
		{ "Title", function()
		
			CharacterCreate.TitleEntry = vgui.Create( "DTextEntry", CharacterCreate.Frame );
			CharacterCreate.TitleEntry:SetPos( x + 50, y + 75 );
			CharacterCreate.TitleEntry:SetSize( 150, 15 );
			CharacterCreate.TitleEntry:MakePopup();
			
			table.insert( CharacterCreate.Fields, CharacterCreate.TitleEntry )

		end },
		
		
		{ "Title2", function()
		
			CharacterCreate.Title2Entry = vgui.Create( "DTextEntry", CharacterCreate.Frame );
			CharacterCreate.Title2Entry:SetPos( x + 65, y + 105 );
			CharacterCreate.Title2Entry:SetSize( 150, 15 );
			CharacterCreate.Title2Entry:MakePopup();
		
		end },
		
		
		{ "Models", function()
			
			CharacterCreate.ModelPanel = CreateBPanel( nil, 13, 130, 337, 83 );
			CharacterCreate.ModelPanel:SetParent( CharacterCreate.Frame );
			CharacterCreate.ModelPanel:EnableScrolling( true )
			
			for k, v in pairs( HL2Factions[faction].Models ) do
			
				spawnicon = vgui.Create( "SpawnIcon",CharacterCreate.ModelPanel );
				spawnicon:SetPos( 3 + modelx, 5 + modely );
				spawnicon:SetIconSize( 32, 32 );
				spawnicon:SetModel( v );
				spawnicon.DoClick = function()
				
					ChosenCharacterModel = v
					
					if( CharacterCreate.modelpnl ) then
						CharacterCreate.modelpnl:Remove()
					end	
					
					CharacterCreate.modelpnl = vgui.Create( "DModelPanel" );
					CharacterCreate.modelpnl:SetParent( PlayerMenuPanel );
					
					CharacterCreate.modelpnl:SetPos( ScrW() * .5 + 140, 240 );
					CharacterCreate.modelpnl:SetSize( 400, 400 );
					
					CharacterCreate.modelpnl:SetModel( ChosenCharacterModel );
					CharacterCreate.modelpnl.Model = ChosenCharacterModel

					CharacterCreate.modelpnl.Think = function()
					
						if( CharacterCreate.modelpnl.Model ~= v ) then
						
							CharacterCreate.modelpnl:SetModel( v );
							CharacterCreate.modelpnl.Model = v
							
							local seq = CharacterCreate.modelpnl.Entity:SelectWeightedSequence( 1 );
							CharacterCreate.modelpnl.Entity:ResetSequence( seq );
							
						end
					
					end

					CharacterCreate.modelpnl:SetCamPos( Vector( 62, 1, 43 ) );
					CharacterCreate.modelpnl:SetLookAt( Vector( -23, -2, 37 ) );
					CharacterCreate.modelpnl:SetFOV( 86 );

					CharacterCreate.modelpnl.LayoutEntity = function( self ) 
					
						if( self.Entity:GetSequence() <= 0 ) then
					
							local seq = CharacterCreate.modelpnl.Entity:SelectWeightedSequence( 1 );
							CharacterCreate.modelpnl.Entity:ResetSequence( seq );
							
						end
					
						CharacterCreate.modelpnl:RunAnimation();
					
					end

					CharacterCreate.modelpnl.Entity:ResetSequence( -1 );
					
					CharacterCreate.FinishLink = vgui.CreateLink();
					CharacterCreate.FinishLink:SetParent( CharacterCreate.Header );
					CharacterCreate.FinishLink:SetPos( 10, 40 );
					CharacterCreate.FinishLink:SetHighlightColor( Color( 0, 255, 0, 255 ) );
					CharacterCreate.FinishLink:SetText( "Finish", "NewChatFont" );
					CharacterCreate.FinishLink.Action = function()
					
						if( string.len( CharacterCreate.NameEntry:GetValue() ) < 7 or
							string.len( CharacterCreate.NameEntry:GetValue() ) > 38 ) then
						
							CreateOkPanel( "Invalid name.", "Your name needs to be between 7 and 38 characters." );
							return;		
						
						end
					
						if( string.gsub( CharacterCreate.NameEntry:GetValue(), " ", "" ) == "" ) then
						
							CreateOkPanel( "Invalid name.", "Your name is blank, queer." );
							return;		
						
						end
					
						if( not tonumber( CharacterCreate.AgeEntry:GetValue() ) ) then
						
							CreateOkPanel( "Invalid number", "Invalid age." );
							return;
						
						end
						
						if( tonumber( CharacterCreate.AgeEntry:GetValue() ) < 18 or
							tonumber( CharacterCreate.AgeEntry:GetValue() ) > 80 ) then
							
							CreateOkPanel( "Invalid number", "Age must be between 18 and 80." );
							return;			
							
						end
					
						if( ChosenCharacterModel == "" ) then
						
							CreateOkPanel( "Invalid model", "Choose a model." );
							return;
						
						end
								
						if( not LocalPlayer():GetTable().InGame ) then
							RunConsoleCommand( "eng_createcharacter", faction, CharacterCreate.NameEntry:GetValue(), CharacterCreate.AgeEntry:GetValue(), CharacterCreate.TitleEntry:GetValue(), CharacterCreate.Title2Entry:GetValue(), ChosenCharacterModel );
						else
							RunConsoleCommand( "eng_createcharacter", faction, CharacterCreate.NameEntry:GetValue(), CharacterCreate.AgeEntry:GetValue(), CharacterCreate.TitleEntry:GetValue(), CharacterCreate.Title2Entry:GetValue(), ChosenCharacterModel );
							RunConsoleCommand( "rp_closecharcreate" );							
						end	
						
						CharacterCreate.Header:Remove()
						CharacterCreate.Frame:Remove()
						CharacterCreate.modelpnl:Remove()
						CharacterCreate.ModelPanel:Remove()
						CharacterCreate.ModelPanel = nil
						
						for i, o in pairs( CharacterCreate.Fields ) do
							CharacterCreate.Fields[i]:Remove()
						end
											
						CharacterCreate.FactionNameLabel:FadingOut( 255 )
						CharacterCreate.FactionNameLabel:FadeDelay( .5 )
						
						CharacterCreate.FinishLink:FadingOut( 255 )
						CharacterCreate.FinishLink:FadeDelay( 1 )
						
						for u, y in pairs( CharacterCreate.LinkFields ) do
							CharacterCreate.LinkFields[u]:FadingOut( 150 )
						end

						timer.Simple( 5, function()

							for u, y in pairs( CharacterCreate.LinkFields ) do
								CharacterCreate.LinkFields[u]:Remove()
							end
							
							CharacterCreate.FactionNameLabel:Remove()
							CharacterCreate.FinishLink:Remove()
							
							
							
						end )	
			
					end	
					
				end	
				
				modelx = modelx + 33
				
				if( modelx > 320 ) then
					modelx = 0
					modely = modelx + 33
				end	
				
			end	

		end },
		
	}	
	
	for k, v in pairs( fields ) do
	
		CharacterCreate.LinkFields[k] = vgui.CreateLink();
		CharacterCreate.LinkFields[k]:SetParent( CharacterCreate.Frame );
		CharacterCreate.LinkFields[k]:SetPos( 15, 15 + CharacterCreate.yoffset );
		CharacterCreate.LinkFields[k]:SetText( v[1], "NewChatFont" )
		CharacterCreate.LinkFields[k]:FadingIn( 180 );
		CharacterCreate.LinkFields[k]:FadeDelay( CharacterCreate.FadeDelay );
		CharacterCreate.LinkFields[k].Action = v[2]

		CharacterCreate.LinkFields[k]:SetHighlightColor( Color( 120, 0, 0, 255 ) );
		
		CharacterCreate.yoffset = CharacterCreate.yoffset + 30
		CharacterCreate.FadeDelay = CharacterCreate.FadeDelay + .4
		
	end	

end

function event.CreateChooseMenu()
	
	RunConsoleCommand( "eng_getchars", UniqueID );
	CharacterSaves = { };
	
	local canclose;

	ChooseMenu = CreateBPanel( "Load Character", ScrW() / 2 - 150, math.Clamp( ScrH() / 2 - 210, ScrH() * .1 + 70, 9999 ), 180, 300 );
	ChooseMenu:CanClose( true );
	ChooseMenu:CanDrag( false );
	ChooseMenu:SetBodyColor( Color( 40, 40, 40, 220 ) );
	
	ChooseMenu.OnClose = function()
	
		if( not LocalPlayer():GetTable().InGame ) then return end
	
		if( LocalPlayer():GetTable().InGame ) then
		
			RunConsoleCommand( "rp_closecharcreate" );
			CharacterSaves = { }
			TS.MapView = false
			
		else

			ChooseMenu:Remove()
			CharacterSaves = { }
			TS.MapView = false
			
		end

	end	
	
	ChooseMenu.Frame = CreateBPanel( nil, 8, 5, 162, 250 )
	ChooseMenu.Frame:SetParent( ChooseMenu )
	ChooseMenu.Frame:SetBodyColor( Color( 30, 30, 30, 255 ) );
	ChooseMenu.Frame:EnableScrolling( true )
	
	local y = 0
	
	ChooseMenu.Frame.PaintHook = function()
	
		for k, v in pairs( CharacterSaves ) do
				
			spawnicon = vgui.Create( "SpawnIcon", ChooseMenu );
			spawnicon:SetModel( v.Model );
			spawnicon:SetIconSize( 32, 32 ) 
			spawnicon:SetPos( 15, y + 30 )
			spawnicon:SetToolTip( v.Name );
			spawnicon.DoClick = function()
			
				DeletePanel = CreateBPanel( "Delete save", ( ScrW() * .5 ) - 100, ( ScrH() * .5 ) - 25, 200, 50 );
				DeletePanel:CanClose( true );
				DeletePanel:CanDrag( false );
				DeletePanel:SetBodyColor( Color( 40, 40, 40, 220 ) );
				
				DeletePanel:AddLabel( "Delete " .. v.Name .. "?", "NewChatFont", 8, 8, Color( 255, 255, 255, 255 ) );
				
				DeletePanel.Yes = DeletePanel:AddButton( "Yes", 70, 25);
				
				DeletePanel.Yes.Action = function()
					
					RunConsoleCommand( "eng_removechar", UniqueID, v.ID );
					
					CharacterSaves = { }
					
					if( ChooseMenu ) then
						ChooseMenu:Remove()
						ChooseMenu = nil
					end	
					
					event.CreateChooseMenu()
					
					DeletePanel:Remove()
				
				end
				
				DeletePanel.No = DeletePanel:AddButton( "No", 105, 25);
				
				DeletePanel.No.Action = function()
					
					if( DeletePanel and DeletePanel.TitleBar ) then
				
					DeletePanel.TitleBar:Remove();
					DeletePanel:Remove();

					end
				
				end
				
			end	
			
			local nick = v.Name;
			
			if( string.len( nick ) > 10 ) then
				nick = string.sub( nick, 1, 10 ) .. "..";
			end
		
			ChooseMenu.SelectBut = ChooseMenu:AddButton( nick, 50, y + 33 )
			ChooseMenu.SelectBut.Action = function()
		
				RunConsoleCommand( "eng_selectchar", UniqueID, v.ID );
				RunConsoleCommand( "rp_closecharcreate" )
			
			end
			
			CharacterSaves = { };
			
			y = y + 34
			
			ChooseMenu.Frame:AddObject( spawnicon )
			ChooseMenu.Frame:AddObject( ChooseMenu.SelectBut )
			
		end
		
	end	

	ChooseMenu.CreateBut = ChooseMenu:AddButton( "Create new character", 13, 275 );
	ChooseMenu.CreateBut.Action = function()
		event.StageOneCharacterCreation()
		if( ChooseMenu ) then

			if( ChooseMenu.TitleBar ) then
				ChooseMenu.TitleBar:Remove();
				ChooseMenu.TitleBar = nil;	
			end
			ChooseMenu:Remove();
			ChooseMenu = nil;
		end
	end	

	gui.EnableScreenClicker( true );
	
end	

function RemoveYesNoDel()
	
	if( YesNo and YesNo.TitleBar ) then
	
		YesNo.TitleBar:Remove();
		YesNo:Remove();

	end

end
usermessage.Hook( "RYNDEL", RemoveYesNoDel );
